<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="留言人姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入留言人姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="留言人电话号码" prop="telephone">
        <el-input
          v-model="queryParams.telephone"
          placeholder="请输入留言人电话号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入邮箱"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable size="small">
          <el-option
            v-for="dict in dict.type.leave_message_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.leave_message_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="提交日期" prop="submitDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.submitDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择提交日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="处理日期" prop="dealedDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.dealedDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择处理日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="来源" prop="sourceOfMessage">
        <el-select v-model="queryParams.sourceOfMessage" placeholder="请选择来源" clearable size="small">
          <el-option
            v-for="dict in dict.type.leave_message_source_of_message"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="对象" prop="leaveOfObject">
        <el-select v-model="queryParams.leaveOfObject" placeholder="请选择对象" clearable size="small">
          <el-option
            v-for="dict in dict.type.leave_message_leave_of_object"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="处理人的账号" prop="dealPersonAccount">
        <el-input
          v-model="queryParams.dealPersonAccount"
          placeholder="请输入处理人的账号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="满意度" prop="satisfactionOfPerson">
        <el-input
          v-model="queryParams.satisfactionOfPerson"
          placeholder="请输入满意度"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="被建议人姓名" prop="suggestObjectName">
        <el-input
          v-model="queryParams.suggestObjectName"
          placeholder="请输入被建议人姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="被建议人电话" prop="suggestObjectTelephone">
        <el-input
          v-model="queryParams.suggestObjectTelephone"
          placeholder="请输入被建议人电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['message:leaveMessage:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['message:leaveMessage:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['message:leaveMessage:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['message:leaveMessage:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="leaveMessageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="留言id" align="center" prop="leaveMessageId" />
      <el-table-column label="留言人姓名" align="center" prop="name" />
      <el-table-column label="留言人电话号码" align="center" prop="telephone" />
      <el-table-column label="邮箱" align="center" prop="email" />
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.leave_message_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.leave_message_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="提交日期" align="center" prop="submitDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.submitDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处理日期" align="center" prop="dealedDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dealedDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="来源" align="center" prop="sourceOfMessage">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.leave_message_source_of_message" :value="scope.row.sourceOfMessage"/>
        </template>
      </el-table-column>
      <el-table-column label="对象" align="center" prop="leaveOfObject">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.leave_message_leave_of_object" :value="scope.row.leaveOfObject"/>
        </template>
      </el-table-column>
      <el-table-column label="处理人的账号" align="center" prop="dealPersonAccount" />
      <el-table-column label="满意度" align="center" prop="satisfactionOfPerson" />
      <el-table-column label="被建议人姓名" align="center" prop="suggestObjectName" />
      <el-table-column label="被建议人电话" align="center" prop="suggestObjectTelephone" />
      <el-table-column label="留言内容" align="center" prop="text" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['message:leaveMessage:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['message:leaveMessage:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改留言对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="留言人姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入留言人姓名" />
        </el-form-item>
        <el-form-item label="留言人电话号码" prop="telephone">
          <el-input v-model="form.telephone" placeholder="请输入留言人电话号码" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option
              v-for="dict in dict.type.leave_message_type"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.leave_message_status"
              :key="dict.value"
:label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="提交日期" prop="submitDate">
          <el-date-picker clearable size="small"
            v-model="form.submitDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择提交日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="处理日期" prop="dealedDate">
          <el-date-picker clearable size="small"
            v-model="form.dealedDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择处理日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="来源" prop="sourceOfMessage">
          <el-select v-model="form.sourceOfMessage" placeholder="请选择来源">
            <el-option
              v-for="dict in dict.type.leave_message_source_of_message"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="对象" prop="leaveOfObject">
          <el-select v-model="form.leaveOfObject" placeholder="请选择对象">
            <el-option
              v-for="dict in dict.type.leave_message_leave_of_object"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="处理人的账号" prop="dealPersonAccount">
          <el-input v-model="form.dealPersonAccount" placeholder="请输入处理人的账号" />
        </el-form-item>
        <el-form-item label="满意度" prop="satisfactionOfPerson">
          <el-input v-model="form.satisfactionOfPerson" placeholder="请输入满意度" />
        </el-form-item>
        <el-form-item label="被建议人姓名" prop="suggestObjectName">
          <el-input v-model="form.suggestObjectName" placeholder="请输入被建议人姓名" />
        </el-form-item>
        <el-form-item label="被建议人电话" prop="suggestObjectTelephone">
          <el-input v-model="form.suggestObjectTelephone" placeholder="请输入被建议人电话" />
        </el-form-item>
        <el-form-item label="留言内容" prop="text">
          <el-input v-model="form.text" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLeaveMessage, getLeaveMessage, delLeaveMessage, addLeaveMessage, updateLeaveMessage } from "@/api/message/leaveMessage";

export default {
  name: "LeaveMessage",
  dicts: ['leave_message_type', 'leave_message_status', 'leave_message_source_of_message', 'leave_message_leave_of_object'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 留言表格数据
      leaveMessageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        telephone: null,
        email: null,
        type: null,
        status: null,
        submitDate: null,
        dealedDate: null,
        sourceOfMessage: null,
        leaveOfObject: null,
        dealPersonAccount: null,
        satisfactionOfPerson: null,
        suggestObjectName: null,
        suggestObjectTelephone: null,
        text: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "留言人姓名不能为空", trigger: "blur" }
        ],
        telephone: [
          { required: true, message: "留言人电话号码不能为空", trigger: "blur" }
        ],
        email: [
          { required: true, message: "邮箱不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "类型不能为空", trigger: "change" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],
        submitDate: [
          { required: true, message: "提交日期不能为空", trigger: "blur" }
        ],
        sourceOfMessage: [
          { required: true, message: "来源不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询留言列表 */
    getList() {
      this.loading = true;
      listLeaveMessage(this.queryParams).then(response => {
        this.leaveMessageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        leaveMessageId: null,
        name: null,
        telephone: null,
        email: null,
        type: null,
        status: "0",
        submitDate: null,
        dealedDate: null,
        sourceOfMessage: null,
        leaveOfObject: null,
        dealPersonAccount: null,
        satisfactionOfPerson: null,
        suggestObjectName: null,
        suggestObjectTelephone: null,
        text: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.leaveMessageId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加留言";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const leaveMessageId = row.leaveMessageId || this.ids
      getLeaveMessage(leaveMessageId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改留言";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.leaveMessageId != null) {
            updateLeaveMessage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLeaveMessage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const leaveMessageIds = row.leaveMessageId || this.ids;
      this.$modal.confirm('是否确认删除留言编号为"' + leaveMessageIds + '"的数据项？').then(function() {
        return delLeaveMessage(leaveMessageIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('message/leaveMessage/export', {
        ...this.queryParams
      }, `leaveMessage_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
